package example.controller;

import example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;


/**
 * @Description: 接口测试
 *
 * @author xub
 * @date 2019/8/24 下午6:31
 */
@RestController
public class OrderController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private OrderService orderService;
    @RequestMapping("save-order")
    public Object saveUser(HttpServletRequest request,HttpServletResponse httpServletResponse){
        String order_id =  String.valueOf(new Date().getTime());
        int user_id =  new Random().nextInt(10);
        jdbcTemplate.update(" insert into t_order(shop_id,user_id,num,amount,order_id,status)value('123456',"+user_id+",10,1000,"+order_id+",1)");
        return "success";
    }
    @GetMapping("list-order")
    public Object listUser(HttpServletRequest request, HttpServletResponse response) {
        List<Map> list = jdbcTemplate.query("select * from t_order order by create_time asc", new RowMapper<Map>() {
            @Override
            public Map mapRow(ResultSet rs, int rowNum) throws SQLException {
                System.out.println(rowNum);
                Map<String, String> row = new HashMap();
                System.out.println(rs.getString("order_id"));
                row.put("user_id", rs.getString("user_id"));
                row.put("order_id", rs.getString("order_id"));
                return row;
            }
        });
        for (int i = 0; i < list.size(); i++) {
            String sql = "";
            if (i == 2) {
                //更新
                sql = " update t_order set update_time = now() where order_id = " + list.get(i).get("order_id");
                jdbcTemplate.update(sql);
            }
            if (i == 3) {
                //更新
                sql = " update t_order set update_time = now() where user_id="+list.get(i).get("user_id");
                jdbcTemplate.update(sql);
            }
            if (i == 0) {
                //删除
                sql = "delete from t_order where order_id = " + list.get(i).get("order_id");
                //jdbcTemplate.update(sql);
            }
            if (i == 1) {
                //删除
                sql = "delete from t_order where user_id = " + list.get(i).get("user_id");
                //jdbcTemplate.update(sql);

            }
        }
        return list;
    }
    @RequestMapping("xa-test")
    public Object XATest(HttpServletRequest request,HttpServletResponse response) throws Exception {
        orderService.insert();
        return "success";
    }
}
